package com.gaoxin.api.contronller.stucontroller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.gaoxin.api.entity.stuentity.DormInfoEntity;
import com.gaoxin.api.entity.stuentity.FamilyInfoEntity;
import com.gaoxin.api.entity.stuentity.SchoolInfoEntity;
import com.gaoxin.api.entity.stuentity.StuInfoEntity;
import com.gaoxin.api.service.stuservice.StuInfoService;
import com.google.common.collect.Lists;

import io.swagger.annotations.Api;
import net.sf.jsqlparser.statement.select.Select;

/**
 * 学生系统接口-控制层
 *
 * @author fanyue
 * @email 859485490@qq.com
 * @date 2018-07-12 15:21
 */
@RestController
@RequestMapping("/app/student")
@Api(tags = "学生系统接口")
public class StuInfoController {

	Logger log = Logger.getLogger("学生系统接口~~~~~~~~~~~log~~~~~~~~");
	@Autowired
	private StuInfoService stuInfoService;

	@RequestMapping(value = "/queryStuInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String Test(@RequestBody Map<String, String> paramMap) {
		String stuNumber = paramMap.get("stuNumber");
		log.info("查询学生信息开始+++++++++" + stuNumber);
		String returnStr = "";
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			StuInfoEntity stuInfo = stuInfoService.queryStuInfo(stuNumber);
			if (stuInfo == null) {
				retMap.put("code", "100100");
				retMap.put("errmsg", "未查询到该学号对应学生");
				return JSON.toJSONString(retMap);
			}
			Map<String, Object> stuInfoMap = new HashMap<String, Object>();
			stuInfoMap.put("stuNumber", stuInfo.getStuNumber());
			stuInfoMap.put("stuName", stuInfo.getStuName());
			stuInfoMap.put("basicInfo", stuInfo);

			SchoolInfoEntity querySchoolInfo = stuInfoService.querySchoolInfo(stuInfo.getStuCode());
			if (querySchoolInfo == null) querySchoolInfo = new SchoolInfoEntity();
			stuInfoMap.put("schoolInfo", querySchoolInfo);

			DormInfoEntity queryDromInfo = stuInfoService.queryDromInfo(stuInfo.getStuCode());
			if (queryDromInfo == null) queryDromInfo = new DormInfoEntity();
			stuInfoMap.put("dormInfo", queryDromInfo);

			FamilyInfoEntity queryFamilyInfo = stuInfoService.queryFamilyInfo(stuInfo.getStuCode());
			if (queryFamilyInfo == null) queryFamilyInfo = new FamilyInfoEntity();
			stuInfoMap.put("familyInfo", queryFamilyInfo);
			stuInfoService.queryStuInfo(stuNumber);
			retMap.put("code", "0");
			retMap.put("results", stuInfoMap);
			returnStr = JSON.toJSONString(retMap);
		} catch (Exception e) {
			log.warning("查询学生信息异常。。。");
			e.printStackTrace();
			retMap.put("code", "100101");
			retMap.put("errmsg", "查询学生信息错误");
			returnStr = JSON.toJSONString(retMap);
		}
		// log.info("returnStr+++++++++" + returnStr);
		return returnStr;
	}
	@RequestMapping(value = "/queryStudentByStudent", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryStudentbyStudent(@RequestBody StuInfoEntity stuInfoEntity) {
//		String stuNumber = paramMap.get("stuNumber");
//		log.info("查询学生信息开始+++++++++" + stuNumber);
		
		String returnStr = "";
		Map<String, Object> retMap = new HashMap<String, Object>();
		if(stuInfoEntity.getStuNumber()==null || "".equals(stuInfoEntity.getStuNumber())) {
			retMap.put("code", "100105");
			retMap.put("errmsg", "请输入查询条件！");
			returnStr = JSON.toJSONString(retMap);
			return returnStr;
		}
		try {
//			StuInfoEntity stuInfo = stuInfoService.queryStuInfo(stuNumber);
//			if (stuInfo == null) {
//				retMap.put("code", "100100");
//				retMap.put("errmsg", "未查询到该学号对应学生");
//				return JSON.toJSONString(retMap);
//			}
//			Map<String, Object> stuInfoMap = new HashMap<String, Object>();
//			stuInfoMap.put("stuNumber", stuInfo.getStuNumber());
//			stuInfoMap.put("stuName", stuInfo.getStuName());
//			stuInfoMap.put("basicInfo", stuInfo);
//
//			SchoolInfoEntity querySchoolInfo = stuInfoService.querySchoolInfo(stuInfo.getStuCode());
//			if (querySchoolInfo == null) querySchoolInfo = new SchoolInfoEntity();
//			stuInfoMap.put("schoolInfo", querySchoolInfo);
//
//			DormInfoEntity queryDromInfo = stuInfoService.queryDromInfo(stuInfo.getStuCode());
//			if (queryDromInfo == null) queryDromInfo = new DormInfoEntity();
//			stuInfoMap.put("dormInfo", queryDromInfo);
//
//			FamilyInfoEntity queryFamilyInfo = stuInfoService.queryFamilyInfo(stuInfo.getStuCode());
//			if (queryFamilyInfo == null) queryFamilyInfo = new FamilyInfoEntity();
//			stuInfoMap.put("familyInfo", queryFamilyInfo);
//			stuInfoService.queryStuInfo(stuInfoEntity);
//			retMap.put("code", "0");
//			retMap.put("results", stuInfoMap);
//			returnStr = JSON.toJSONString(retMap);
			List<StuInfoEntity> list = stuInfoService.queryStuAllinfo(stuInfoEntity);
			if(list.size()!=0&&list!=null) {
				retMap.put("code", "0");
				retMap.put("results", list);
			}else {
				retMap.put("code", "100104");
				retMap.put("errmsg", "查询无结果！");
			}
			returnStr = JSON.toJSONString(retMap);
		} catch (Exception e) {
			log.warning("查询学生信息异常。。。");
			e.printStackTrace();
			retMap.put("code", "100101");
			retMap.put("errmsg", "查询学生信息错误");
			returnStr = JSON.toJSONString(retMap);
		}
		// log.info("returnStr+++++++++" + returnStr);
		return returnStr;
	}
	/**
	 * 查询专业学生成绩
	 * @return
	 */
	@PostMapping(value="/queryStuInfoByMajor",produces = "application/json;charset=UTF-8")
	public String queryStuInfoByMajor (@RequestBody StuInfoEntity stuInfoEntity) {
		String returnStr = "";
		Map<String, Object> retMap = new HashMap<String, Object>();
		if(stuInfoEntity.getDeptId()==null) {
			retMap.put("code", "100102");
			retMap.put("errmsg", "请选择院系！");
			returnStr = JSON.toJSONString(retMap);
			return returnStr;
		}
		try {
			List<StuInfoEntity> list = stuInfoService.queryStuByMajorId(stuInfoEntity);
			if(list.size()!=0&&list!=null) {
				retMap.put("code", "0");
				retMap.put("results", list);
			}else {
				retMap.put("code", "100103");
				retMap.put("errmsg", "查询无结果！");
			}
			returnStr = JSON.toJSONString(retMap);
			return returnStr;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnStr;
	}
}
